package com.dbw.inputmethod;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class QQPinyinLibMaker implements CharCodeLibMaker {

    public QQPinyinLibMaker() {
        
    }

    @Override
    public void make(List<CharCode> charCodeLib, File libFile) throws Exception {
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(libFile), "GBK"));
        Map<String, Integer> codePosMap = new HashMap<>();
        for(CharCode charCode : charCodeLib) {
            String code = charCode.getCode();
            Integer pos = codePosMap.get(code);
            if(pos == null) {
                pos = 1;
            }
            String pinyinDesc = String.format(Locale.getDefault(), 
                    "%s=%d,%s", code, pos, charCode.getChars());
//            String pinyinDesc = String.format(Locale.getDefault(), 
//                    "%s\t%s", code, charCode.getChars());
            writer.write(pinyinDesc);
            writer.newLine();
            pos++;
            codePosMap.put(code, pos);
        }
        writer.close();
    }

}
